#!/usr/bin/python3
# -*- coding: utf-8 -*-

import types
from importlib import import_module
from aiohttp import web, web_routedef
from aioweb import db

# v2ray = import_module('plugins.v2ray')
# route = v2ray.view_v2ray.routes
name = 'user'


def test_view(name):
    module = import_module(f'plugins.{name}.view_{name}')
    route = getattr(module, 'routes')
    for item in route:
        # 只打印RouteDef,避免StaticDef报错
        if isinstance(item, web_routedef.RouteDef):
            print('%-4s %-25s -> %s' %
                  (item.method, item.path, item.handler.__name__))


def test_middlewares(name):
    middlewares = import_module(f'plugins.{name}.middlewares')
    middlewares = [getattr(middlewares, item) for item in dir(middlewares)
                   if isinstance(getattr(middlewares, item), types.FunctionType)]
    print(middlewares)


def test_db(name):
    db_p = import_module(f'plugins.{name}.db')
    # for item in dir(db_p):
    #     if db.istableclass(getattr(db_p, item)):
    #         print(item, type(getattr(db_p, item)))
    db_p = [getattr(db_p, item) for item in dir(
        db_p) if db.istableclass(getattr(db_p, item))]
    print(db_p)


if __name__ == '__main__':
    test_db(name)
